﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IP代理
{
    public partial class IEAgentIP : Form
    {
        public IEAgentIP()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                string ip = dataGridView.CurrentCell.OwningRow.Cells["地址"].Value.ToString();
                string port = dataGridView.CurrentCell.OwningRow.Cells["端口"].Value.ToString();

                bool isb = IPProxy.Proxy(ip, int.Parse(port));

                if (isb)
                {
                    lalMsg.ForeColor = Color.Black;
                    lalMsg.Text = "代理设置成功~." + ip + ":" + port;
                }
                else
                {
                    lalMsg.ForeColor = Color.Red;
                    lalMsg.Text = "此代理无效~." + ip + ":" + port;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void but_add_Click(object sender, EventArgs e)
        {
            //添加一行空白
            string[] row = { "", "", "" };
            dataGridView.Rows.Add(row);
            lalMsg.ForeColor = Color.Black;
            lalMsg.Text = "请输入新的代理.";
            //滚动到最后一行
            dataGridView.CurrentCell = dataGridView.Rows[dataGridView.Rows.Count - 1].Cells[0];

            lalMsg.ForeColor = Color.Black;           
        }

        private void AgentManagement_Load(object sender, EventArgs e)
        {
            //初始化dataGridView
            dataGridView.RowHeadersWidth = 33;
            dataGridView.ColumnCount = 3;
            dataGridView.Columns[0].Name = "名称";
            dataGridView.Columns[1].Name = "地址";
            dataGridView.Columns[2].Name = "端口";

            try
            {
                List<string> list = FileHelp.GetFile(Application.ExecutablePath + ".ip");
                foreach (var p in list)
                {
                    if (string.IsNullOrEmpty(p) || p.Split(':').Length < 3)
                        continue;
                    string[] row = { p.Split(':')[0], p.Split(':')[1], p.Split(':')[2] };
                    dataGridView.Rows.Add(row);                         //添加一行
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void but_del_Click(object sender, EventArgs e)
        {
            if (dataGridView.Rows.Count <= 0)
            {
                lalMsg.ForeColor = Color.Red;
                lalMsg.Text = "表已经为空";
                return;
            }
            dataGridView.Rows.Remove(dataGridView.CurrentRow);
            lalMsg.ForeColor = Color.Black;
            lalMsg.Text = "删除成功~";
        }

        private void but_no_Click(object sender, EventArgs e)
        {
            IPProxy.CancelProxy();

            lalMsg.ForeColor = Color.Black;
            lalMsg.Text = "取消成功~";
        }

        private void but_save_Click(object sender, EventArgs e)
        {
            //http://www.ip138.com/
            //http://ip.chinaz.com/
            System.Diagnostics.Process.Start("iexplore.exe", "http://ip.chinaz.com/");
        }

        private void save()
        {
            string str = string.Empty;
            for (int i = 0; i < dataGridView.Rows.Count; i++)
            {
                string name = dataGridView.Rows[i].Cells["名称"].Value.ToString();
                string ip = dataGridView.Rows[i].Cells["地址"].Value.ToString();
                string port = dataGridView.Rows[i].Cells["端口"].Value.ToString();
                str += name + ":" + ip + ":" + port + "\r\n";
            }
            FileHelp.SaveFile(Application.ExecutablePath + ".ip", str);
            lalMsg.ForeColor = Color.Black;
        }

        private void IEAgentIP_FormClosing(object sender, FormClosingEventArgs e)
        {
            save();
        }
    }
}
